#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QDesktopWidget>
#include "networkavdataread.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    buf_show(0,0)
{
    ui->setupUi(this);

    m_pNetworkAvData = new NetworkAvDataRead(*this);

    m_pNetworkAvData->activate();

    pRtmpTask = new RtmpTask(m_pNetworkAvData);

    pRtmpTask->activate();
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::receive_handler(unsigned char *data, int size, double pts)
{
    {
        CCriticalAutoLock loAutoLock(oCriSection);
        buf_show = CStreamBuf(data,size);
    }
    update();

    return true;
}

void MainWindow::paintEvent( QPaintEvent *event )
{
    QMainWindow::paintEvent(event);

    QPainter painter( this );

    {
        desktopRc = QApplication::desktop()->availableGeometry();

        QRect rcshow = QRect(0,0,m_pNetworkAvData->AvWidth(),m_pNetworkAvData->Avheight());
        rcshow.moveCenter(desktopRc.center());

        CCriticalAutoLock loAutoLock(oCriSection);

        unsigned char* ptr = (unsigned char*)buf_show.Buf();
        if(buf_show.Len()>0)
        {
            painter.drawImage(rcshow,/*QPixmap::fromImage*/(QImage(ptr,m_pNetworkAvData->AvWidth(),m_pNetworkAvData->Avheight(),QImage::Format_RGB888)));
        }
    }


}

void MainWindow::closeEvent(QCloseEvent *event)
{
    pRtmpTask ->close();

    m_pNetworkAvData->close();

    //m_pNetworkAvData->close();
    QMainWindow::closeEvent(event);
}

